<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>时间工具类</title>
	<script type="text/javascript" src='../lib/bower_components/zepto/zepto.js'></script>

</head>
<body>
	 <p class="time-wrap">
            <span class="num" id="days"></span><span>天</span>
            <span class="num" id="hours"></span><span>小时</span>
            <span class="num" id="minutes"></span><span>分</span>
            <span class="num" id="second"></span><span>秒</span>
        </p>



    <script type="text/javascript">
var time = {
  getRTime: function() {
    var endTime = new Date('2016/12/6 7:00:00');
    var nowTime = new Date();
    var t = endTime.getTime() - nowTime.getTime();
    var d = 0;
    var h = 0;
    var m = 0;
    var s = 0;
    console.log(t);
    if (t > 0) {
      d = Math.floor(t / 1000 / 60 / 60 / 24);
      h = Math.floor(t / 1000 / 60 / 60 % 24);
      m = Math.floor(t / 1000 / 60 % 60);
      s = Math.floor(t / 1000 % 60);
      $("#days").text(d);
      $("#hours").text(h);
      $("#minutes").text(m);
      $("#second").text(s);
      $("#time-wrap").show();
    } else { // $("#page-main").show();
      return;
    }
  },
  getDateDiff: function(d1, d2) {
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var now1 = new Date(d1).getTime();
    var now2 = new Date(d2).getTime();
    var diffValue = now2 - now1;
    if (diffValue < 0) { //若日期不符则弹出窗口告之
      //alert("结束日期不能小于开始日期！");
    }
    var monthC = diffValue / month;
    var weekC = diffValue / (7 * day);
    var dayC = diffValue / day;
    var hourC = diffValue / hour;
    var minC = diffValue / minute;
    if (monthC >= 1) {
      result = "发表于" + parseInt(monthC) + "个月前";
    } else if (weekC >= 1) {
      result = "发表于" + parseInt(weekC) + "周前";
    } else if (dayC >= 1) {
      result = "发表于" + parseInt(dayC) + "天前";
    } else if (hourC >= 1) {
      result = "发表于" + parseInt(hourC) + "个小时前";
    } else if (minC >= 1) {
      result = "发表于" + parseInt(minC) + "分钟前";
    } else result = "刚刚发表";
    return result;
  },
  convertTime: function(input) { //传入的时间为空或小于0
    if (input == null || input < 0) {
      return;
    }
    var hours = Math.floor(input / 60 / 60);
    var minutes = Math.floor(input / 60 % 60);
    var seconds = input % 60;
    return hours > 0 ? format(hours) + ':' + format(minutes) + ':' + format(seconds) : format(minutes) + ':' + format(seconds);
    function format(n) {
      return n < 10 ? '0' + n: n;
    }
  }
}
  

     
        time.getRTime();
        var nw=time.getDateDiff("2015/07/01 15:25:38","2015/07/01 18:04:20");
        console.log("getDateDiff:"+nw);
        console.log("convertTime:"+time.convertTime(15));
    
    </script>        
</body>
</html>